package com.opgea.crm.web.controller;


import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.opgea.crm.common.vo.OpportunityVo;
import com.opgea.crm.domain.modal.JsonModelMap;
import com.opgea.crm.services.OpportunityService;
import com.opgea.crm.services.SetupService;

@Controller
@RequestMapping(value="searchopportunities")
public class OpportunitySearchController {
    
	Logger logger=Logger.getLogger(OpportunitySearchController.class);
	
	@Autowired
	SetupService setupService;
	@Autowired
	OpportunityService opportunityService;
	
	
	
	@RequestMapping(method=RequestMethod.GET ,value="showOpportunities")
	public @ResponseBody
	Map<String, Object> opportunityList(
			@RequestParam(required=false, value="criteria") String criteria,
			@RequestParam(required=false, value="value")String value){
		
		logger.debug("OpportunitySearchController >> showOpportunities");
		if(criteria != null && value != null){
			List<OpportunityVo> opportunities = opportunityService.findOpportunityList(criteria, value);
			return JsonModelMap.success().data(opportunities);	
		}else{
			return JsonModelMap.success();
		}
	}
	
	@RequestMapping(method=RequestMethod.GET ,value="showOpportunitiesByLead")
	public @ResponseBody
	Map<String, Object> opportunityListByLead(@RequestParam(required=false, value="leadId") String leadId){
		
		logger.debug("OpportunitySearchController >> showOpportunity");
		if(leadId != null){
			List<OpportunityVo> opportunities = opportunityService.findAllByLeadId(new Long(leadId));
			return JsonModelMap.success().data(opportunities);
		}else{
			return JsonModelMap.success();
		}
	}
	
	
	 @RequestMapping(method=RequestMethod.GET, value="oppSearchTypes")
	    public @ResponseBody Map<String, Object> oppSearchTypes(){
	 	   return JsonModelMap.success().data(opportunityService.getOpportunitySearchCriteriaTypes());
	    }
	
}
